<?php
namespace Home\Controller;
use Think\Controller;
vendor('taobao.TopSdk');//引入淘宝SDK
class GoodsListController extends Controller {
	public function __construct() {
		parent::__construct();
		$ti = M('ti') -> where("user_id = '".$_SESSION['user']['id']."'") -> find();
		
		if(empty($ti) || empty($_SESSION['user']['id']))
		{
			$this->redirect('Home/Login/login');
		}
    }
	
    public function index(){
		$visitor_id = $_SESSION['user']['id'];
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$shop_name=empty($_REQUEST['shopname'])?'':trim($_REQUEST['shopname']);
		$name=empty($_REQUEST['name'])?'id':$_REQUEST['name'];
		$asc=empty($_REQUEST['asc'])?'desc':trim($_REQUEST['asc']);
		$model = M('shop');
		//var_dump($shop_name);
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where= "visitor_id in($s_str)";
		}else{
			$where = "visitor_id = '$visitor_id'";
		}
		$shop_list = $model->where($where)->select();
		
		//var_dump($shop_list);
		$result = $this->get_taobao_goods('');
	
		//var_dump($result);
		$goods_list = array();
		foreach($result as $k => $v){
			foreach($v as $key => $val){
				if(isset($val['msg'])){
					$error_list[] = $val;
					continue;
				}
				$goods_list[] = $val;
			}
		}
		//$shop =D('goods_list'); 
		//$result=$shop->shop_list($pageNo,$pageSize,$shop_name,$name,$asc);
		//$this->assign("shop_list",$result['shop_list']);
		
		$arr['count'] = sizeof($goods_list);
		$arr['pageNo'] = $pageNo;
		$arr['pageSize'] = $pageSize;
		$arr['countpage']	= ceil(sizeof($goods_list)/$pageSize);
		$start = ($pageNo-1)*$pageSize; 
		$this->assign("count",$arr);
		
		$info = array_slice($goods_list,$start,$pageSize);
		//var_dump($info);
		$order['name'] = $name;
		$order['asc'] = $asc;
		$this->assign("error_list",$error_list);
		$this->assign('order',$order);
		$this->assign("info",$info);
		$this->assign('shop_name',$shop_name);
		$this->assign("jumpurl",$_SERVER['HTTP_REFERER']);
		$this->assign('shop_list',$shop_list);
		$this->display();
    }
	//导入数据库
	public function insert(){
		$visitor_id = $_SESSION['user']['id'];  
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
		}
		$model = M('goods');
		$num_iid = I("num_iid");
		$cid = I('cid');
		$nick = I('nick');
		$num = I('num');
		$title = I("title");
		$price = I('price');
		$outer_id = I('outer_id');
		if(sizeof($num_iid) > 0){
			$i = 0;
			foreach($num_iid as $k => $v){
				$data['num_iid'] = $v;
				$data['cid'] = $cid[$k];
				$data['nick'] = $nick[$k];
				$data['title'] = $title[$k];
				$data['code'] = $outer_id[$k];
				$data['price'] = $price[$k];
				$data['modify_time'] = date("Y-m-d H:i:s",time());
			
				
				$find = $model->where(" num_iid = ".$v)->find();
				if($find){
					$where= "id =".$find['id'];
					if($shouquan['group_id']){
						$where= "visitor_id in($s_str)";
					}else{
						$where= "visitor_id = $visitor_id";
					}
					$resu = $model->where($where)->save($data);
					
				}else{
					
					$data['add_time'] = date("Y-m-d H:i:s",time());
					$data['stock'] = $num[$k];
					$data['visitor_id'] = $visitor_id;
					$resu = $model->add($data);
				}
				$i++;
			}
			if($i == sizeof($num_iid)){
				echo "<script>window.location.href='".$_SERVER['HTTP_REFERER']."'</script>";
			}else{
				echo "<script>window.location.href='".$_SERVER['HTTP_REFERER']."'</script>";
			}
		}
	}
	//获取列表
	public function get_taobao_goods($shop_name) {
		$visitor_id = $_SESSION['user']['id'];   
		$model = M('shouquan');
		$where = 1;
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where.= " and visitor_id in($s_str)";
		}else{
			$where .= " and visitor_id = '$visitor_id'";
		}
		$user_list = $model->where($where)->select();
	
		//var_dump($user_list);die;
		foreach($user_list as $k => $v){
			$v['top_session'] = base64_decode($v['top_session']);
			
			$c = new \TopClient;
			
			$c->appkey = '23416550';//'23344775'
			$c->secretKey = 'ebbc68fc906b6436f76b4f9cda829976';//'ad2c1882c414be72039d200e494db6a3';
			
			
			$req = new \ItemsOnsaleGetRequest;
			$req->setFields("num_iid,title,nick,cid,num,price,outer_id");
			//var_dump($c);die;
			$resp = $c->execute($req,$v['top_session']);
			if(isset($resp->items->item)){
				$result = $this->object_to_array($resp->items);
				foreach($result['item'] as $key => $val){
					$res[$k][$key] = $val;
				}
			}else{
				$result = $this->object_to_array($resp);
				$res[$k][$key]['nick'] = $v['shop_name'];
				$res[$k][$key]['msg'] = $result['msg'];
			}
		}
		return $res;
	}
	//xml 转数组
	public  function object_to_array($obj){
	  $_arr = is_object($obj) ? get_object_vars($obj) :$obj;
	  foreach ($_arr as $key=>$val){
	   $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val):$val;
	   $arr[$key] = $val;
	  }
	  return $arr;
	 }



}











